local path "C:\Users\pgi1\Dropbox\Coal 2021 _ Joe Peter\Bank Regressions\"
cd "`path'\data"
set seed 100
set more off
clear



use  deals_vars_dataset, clear


bysort packageid ultimateparentid : keep if _n==1


collapse (first) lender (count) loans=deal_amount_converted (sum) deal_amount_converted, by(ultimateparentid )
gsort - loans
format deal_amount_converted  %15.0gc



use  loan_year_vars_dataset, clear

//edit if lender=="BofA Securities"

bysort ultimateparentid year: keep if _n==1
xtset ultimateparentid year

keep if year>=2000 & year<=2020
xtreg loans_mtr_ln treated  loans_total_lag3_ln  loans_related_lag3_ln  y_*, fe vce(cluster ultimateparentid)
keep if e(sample)

replace deal_amount_mtr =deal_amount_mtr /1000000

label var loans_mtr "Number of MTR Loans"
label var deal_amount_mtr "MTR Loan Ammount"
label var loans_total_lag3 "Total Loans Over 3 Years"
label var loans_related_lag3 "Coal Related Loans Over 3 Years"


estpost summarize loans_mtr deal_amount_mtr treated treated_company  treated_project treated_enhanced loans_total_lag3 loans_related_lag3  , de 

esttab using "`path'\tables\table2-summary-stats.rtf",  cells(" mean(fmt(%15.2fc)) sd(fmt(%15.2fc))  p50(fmt(%15.0fc)) count(fmt(%9.0fc))") noobs replace label 


estpost summarize loans_mtr deal_amount_mtr treated treated_company  treated_project treated_enhanced loans_total_lag3 loans_related_lag3  if treated_ever==1, de 

esttab using "`path'\tables\table2-summary-stats.rtf",  cells(" mean(fmt(%15.2fc))  count(fmt(%9.0fc))") noobs append label 


estpost summarize loans_mtr deal_amount_mtr treated treated_company  treated_project treated_enhanced loans_total_lag3 loans_related_lag3  if treated_ever==0, de 

esttab using "`path'\tables\table2-summary-stats.rtf",  cells(" mean(fmt(%15.2fc))  count(fmt(%9.0fc))") noobs append label 


xtreg loans_mtr_ln treated  log_assets asset_growth_3 loans_total_lag3_ln  loans_related_lag3_ln y_*, fe vce(cluster ultimateparentid)
keep if e(sample)

label var assets_lagged "Total Assets ($ mill)"
label var  asset_growth_3 "Asset Growth Over 3 Years"

estpost summarize assets_lagged asset_growth_3  , de 

esttab using "`path'\tables\table2-summary-stats.rtf",  cells(" mean(fmt(%15.2fc)) sd(fmt(%15.2fc))  p50(fmt(%15.0fc)) count(fmt(%9.0fc))") noobs append label 

estpost summarize assets_lagged asset_growth_3  if treated_ever==1, de 

esttab using "`path'\tables\table2-summary-stats.rtf",  cells(" mean(fmt(%15.2fc))  count(fmt(%9.0fc))") noobs append label 



estpost summarize assets_lagged asset_growth_3  if treated_ever==0, de 

esttab using "`path'\tables\table2-summary-stats.rtf",  cells(" mean(fmt(%15.2fc))  count(fmt(%9.0fc))") noobs append label 


exit

use deals_vars_dataset,clear
bysort packageid ultimateparentid : keep if _n==1


keep if year>=2000 & year<=2020

areg deal_amount_mtr_ln treated loans_related_lag3_ln loans_total_lag3_ln log_assets asset_growth_3 y_*, vce(cluster ultimateparentid) absorb(ultimateparentid )
keep if e(sample)

bysort packageid : keep if _n==1


replace deal_amount_converted=deal_amount_converted/1000000

estpost summarize deal_amount_converted maturity revolver secured_loan allindrawn_max      , de 

esttab using "`path'\tables\table2-summary-stats.rtf",  cells(" mean(fmt(%12.2fc)) sd(fmt(%12.2fc))  p50(fmt(%12.0fc)) count(fmt(%9.0fc))") noobs append label 

